package com.fancon.android.gardenjokesx.dialog;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

import com.fancon.android.core.IFanconGlobalState;
import com.fancon.android.emoji.widget.RichTextView;
import com.fancon.android.gardenjokesx.R;
import com.fancon.android.gardenjokesx.entity.User;
import com.fancon.android.gardenjokesx.rpc.GardenJokesApiConnect;
import com.fancon.android.gardenjokesx.rpc.JsonPaser;
import com.fancon.android.multithread.RequestUI;

public class M04MDialog extends Dialog {
	private User mUser;
	private Context mContext;
	private User uData;

	public M04MDialog(Context context, User user) {
		super(context, android.R.style.Theme_Translucent_NoTitleBar);
		// TODO Auto-generated constructor stub
		mUser = user;
		mContext = context;
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.m04m_dialog);
		UserDataRequest userData = new UserDataRequest("m04_user_photo",
				(Activity) mContext);
		((IFanconGlobalState) mContext).getRequestQueue().addRequest(userData);
	}

	private void loadProfile(User user) {
		ImageView close = (ImageView) findViewById(R.id.m04m_p_close);
		close.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				if (event.getAction() == MotionEvent.ACTION_UP) {
					dismiss();
				}
				return true;
			}
		});
		try {
			

			TextView gender = (TextView) findViewById(R.id.m04_m_d_gender);
			TextView birthday = (TextView) findViewById(R.id.m04_m_d_birthday);
			TextView blood = (TextView) findViewById(R.id.m04_m_d_blood);
			TextView home = (TextView) findViewById(R.id.m04_m_d_home);
			RichTextView about = (RichTextView) findViewById(R.id.m04_m_d_about);
			if (user.getPublic_gender().equals("1")) {
				// Show icon
				View v = findViewById(R.id.m04_m_d_gender_img);
				v.setVisibility(View.VISIBLE);

				gender.setVisibility(View.VISIBLE);
				gender.setText(user.getMale().equals("1") ? mContext
						.getString(R.string.S02_male) : mContext
						.getString(R.string.S02_female));
			} else {
				View v = findViewById(R.id.m04_m_d_gender_img);
				v.setVisibility(View.GONE);
				gender.setVisibility(View.GONE);
			}
			String language = Locale.getDefault().getDisplayLanguage();
			if (language.equals("日本語")) {
				if (user.getPublic_birthday() != null
						&& user.getPublic_birthday().equals("1")) {
					// Show icon
					View v = findViewById(R.id.m04_m_d_birthday_img);
					v.setVisibility(View.VISIBLE);

					birthday.setVisibility(View.VISIBLE);

					if (user.getBirthday() != null) {
						String birthDay = user.getBirthday();
						String[] arrSpilted = birthDay.split("/");
						birthDay = arrSpilted[2] + "/" + arrSpilted[0] + "/"
								+ arrSpilted[1];
						birthday.setText(birthDay);
					}
				} else {
					View v = findViewById(R.id.m04_m_d_birthday_img);
					v.setVisibility(View.VISIBLE);
					birthday.setVisibility(View.VISIBLE);
					if (user.getBirthday() != null) {
						String birthDay = user.getBirthday();
						String[] arrSpilted = birthDay.split("/");
						birthDay = arrSpilted[0] + "/" + arrSpilted[1];
						birthday.setText(birthDay);
					}
				}
			} else {
				if (user.getPublic_birthday() != null
						&& user.getPublic_birthday().equals("1")) {
					// Show icon
					View v = findViewById(R.id.m04_m_d_birthday_img);
					v.setVisibility(View.VISIBLE);

					birthday.setVisibility(View.VISIBLE);

					if (user.getBirthday() != null) {
						birthday.setText(user.getBirthday());
					}
				} else {
					View v = findViewById(R.id.m04_m_d_birthday_img);
					v.setVisibility(View.VISIBLE);
					birthday.setVisibility(View.VISIBLE);
					if (user.getBirthday() != null) {
						String birthDay = user.getBirthday();
						String[] arrSpilted = birthDay.split("/");
						birthDay = arrSpilted[1] + "/" + arrSpilted[0];
						birthday.setText(birthDay);
					}
				}
			}

			if ((user.getBlood_type() != null)
					&& (!user.getBlood_type().equals("S"))) {
				// Show icon
				blood.setVisibility(View.VISIBLE);
				View v = findViewById(R.id.m04_m_d_blood_img);

				v.setVisibility(View.VISIBLE);
				if (user.getBlood_type().equals("X")) {
					blood.setText("AB");
				} else {
					blood.setText(user.getBlood_type());
				}
			} else {
				blood.setVisibility(View.GONE);
				View v = findViewById(R.id.m04_m_d_blood_img);
				v.setVisibility(View.GONE);
			}

			if (user.getCurrent_city() != null
					&& user.getCurrent_country() != null) {
				// show icon
				View v = findViewById(R.id.m04_m_d_home_img);
				v.setVisibility(View.VISIBLE);
				if (user.getCurrent_city().length() > 0) {
					home.setText(URLDecoder.decode(user.getCurrent_city())
							+ ", "
							+ URLDecoder.decode((user.getCurrent_country())));
				} else {
					home.setText(URLDecoder.decode((user.getCurrent_country())));
				}
			} else {
				View v = findViewById(R.id.m04_m_d_home_img);
				v.setVisibility(View.GONE);
			}
			about.setText(URLDecoder.decode(user.getAbout()));

		} catch (Exception e) {
			// TODO: handle exception
		}
		
	}

	class UserDataRequest extends RequestUI {
		private Activity context;

		public UserDataRequest(Object key, Activity activity) {
			super(key, activity);
			// TODO Auto-generated constructor stub
			context = activity;
		}

		@Override
		public void execute() throws Exception {
			// TODO Auto-generated method stub
			List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
			nameValuePairs.add(new BasicNameValuePair("user_id", mUser
					.getUser_id()));
			GardenJokesApiConnect api = new GardenJokesApiConnect(context);
			String result = api.execGet(mContext, GardenJokesApiConnect.GROUP_U,
					"getYOUData", nameValuePairs);
			Log.v("StringResult", result);
			JsonPaser jP = new JsonPaser();
			uData = jP.getUser(result);
		}

		@Override
		public void executeUI(Exception ex) {
			// TODO Auto-generated method stub
			loadProfile(uData);
		}

	}
}
